import 'http_exceptions.dart';

/*
返回数据转换后基类
 */
class HttpResponse {
  late bool ok; /// 请求成功
  dynamic? data; /// 返回数据
  String? msg; /// 返回Msg信息
  HttpException? error; /// 错误信息

  HttpResponse._internal({this.ok = false});

  HttpResponse.success(this.data, this.msg) {
    this.ok = true;
  }

  HttpResponse.failure({String? errorMsg, int? errorCode}) {
    this.msg = errorMsg;
    this.error = BadRequestException(message: errorMsg, code: errorCode);
    this.ok = false;
  }

  HttpResponse.failureFormResponse({dynamic? data}) {
    this.error = BadResponseException(data);
    this.ok = false;
  }

  HttpResponse.failureFromError([HttpException? error]) {
    this.error = error ?? UnknownException();
    this.ok = false;
  }
}